import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class LoginServlet
 */
public class LoginServlet extends HttpServlet {
    /**
	 * 
	 */
	private static final long serialVersionUID = 9211568140687018262L;

    public LoginServlet(){
        System.out.println("LoginServlet 构造方法 被调用");
    }
    
    public void init(ServletConfig config) {
        System.out.println("init(ServletConfig)");
    }
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        System.out.println("getRequestURL:"+request.getRequestURL());
        System.out.println("getRequestURI:"+request.getRequestURI());
        System.out.println("getContextPath:"+request.getContextPath());
        System.out.println("浏览器所处于的客户机的主机名:"+request.getRemoteHost());
        System.out.println("服务器的IP地址:"+request.getLocalAddr());
        System.out.println("服务器的主机名:"+request.getLocalName());
        System.out.println("name:" + name);
        System.out.println("password:" + password);
//        String html = null;
//        if ("admin".equals(name) && "123".equals(password))
//            html = "<div style='color:green'>success</div>";
//        else
//            html = "<div style='color:red'>fail</div>";
//        response.setContentType("text/html;charset=utf-8");
//        PrintWriter pw = response.getWriter();
//        pw.println(html);
//        if ("admin".equals(name) && "123".equals(password)) {
        if (!"".equals(name) && !"".equals(password)) {
        	request.getSession().setAttribute("userName", name);
            request.getRequestDispatcher("success.html").forward(request, response);
        }else{
//        	request.getRequestDispatcher("fail.html").forward(request, response);
        	response.sendRedirect("fail.html");
        }
    }
	
    public void destroy() {
        System.out.println("destroy()");
    }
}
